package 回溯;

import java.util.ArrayList;
import java.util.List;

/**
 * todo
 *
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 连续差相同的数字.java
 * @createTime 2023年02月15日 19:35:00
 */
public class 连续差相同的数字 {
	public static void main(String[] args) {
		numsSameConsecDiff(3, 7);
	}

	static List<Integer> result = new ArrayList<>();

	public static int[] numsSameConsecDiff(int n, int k) {
		backTrace(n, 1, k, new int[n]);
		int[] ints = new int[result.size()];
		for (int i = 0; i < result.size(); i++) {
			ints[i] = result.get(i);
			System.out.println(result.get(i));
		}
		return ints;
	}

	public static void backTrace(int n, int index, int k, int[] list) {
		//长度相同
		if (index-1 == n) {
			StringBuilder sb=new StringBuilder();
			for (int i : list) {
				sb.append(i);
			}
			result.add(Integer.parseInt(sb.toString()));
			return;
		}
		for (int i = 0; i < 10; i++) {
			if (index - 1 > 0 && list[0] == 0) {
				break;
			}
			if (index > 1 && Math.abs(list[index - 2] - i) != k) {
				continue;
			}
			list[index - 1] = i;
			backTrace(n, index + 1, k, list);
			list[index - 1] = 0;
		}
	}
}
